//
//  CN_UITextView.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/3/5.
//


import UIKit


public class CN_UITextView: UIView, UITextViewDelegate {

    /// 最大输入字数(当maxNumber == 0无限输入)
    public var maxNumber: Int

    /// textV: 文本输入
    public let textV: UITextView

    /// place: 占位符Label
    public let place: UILabel

    /// numberRemind: 可输入数量Label
    public let numberRemind: UILabel
    
    

    public override init(frame: CGRect) {
        
        maxNumber = 0
        textV = UITextView(frame: frame)
//        textV.isScrollEnabled = false
//        textV.keyboardAppearance = .default
        place = UILabel()
        numberRemind = UILabel()
        numberRemind.font = UIFont.systemFont(ofSize: 13)
        numberRemind.textColor = UIColor(hexString: "999999")
        super.init(frame: frame)
        initialize()
    }

    public convenience init(frame: CGRect = .zero, font: UIFont, textColor: UIColor, bgColor: UIColor, placeFont: UIFont, placeText: String, placeTextColor: UIColor, maxNumber: Int) {
        self.init(frame: frame)
        self.maxNumber = maxNumber
        textV.font = font
        textV.textColor = textColor
        textV.backgroundColor = bgColor
        place.font = placeFont
        place.text = placeText
        place.textColor = placeTextColor
        
        numberRemind.font = UIFont.systemFont(ofSize: 13)
        numberRemind.textColor = UIColor(hexString: "999999")
        updateNumberRemind()
        updatePlaceVisibility()
    }

    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func initialize() {
//        addSubview(textV)
//        addSubview(place)
//        addSubview(numberRemind)
//
//        NSLayoutConstraint.activate([
//            textV.leadingAnchor.constraint(equalTo: leadingAnchor),
//            textV.trailingAnchor.constraint(equalTo: trailingAnchor),
//            textV.topAnchor.constraint(equalTo: topAnchor),
//            textV.bottomAnchor.constraint(equalTo: bottomAnchor),
//            place.leadingAnchor.constraint(equalTo: textV.leadingAnchor, constant: 5),
//            place.trailingAnchor.constraint(equalTo: textV.trailingAnchor),
//            place.topAnchor.constraint(equalTo: textV.topAnchor, constant: 7),
//            numberRemind.trailingAnchor.constraint(equalTo: textV.trailingAnchor, constant: -5),
//            numberRemind.bottomAnchor.constraint(equalTo: textV.bottomAnchor, constant: -7)
//        ])
        
        // 添加textV到视图
            addSubview(textV)
            textV.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                textV.leadingAnchor.constraint(equalTo: leadingAnchor),
                textV.trailingAnchor.constraint(equalTo: trailingAnchor),
                textV.topAnchor.constraint(equalTo: topAnchor),
                textV.bottomAnchor.constraint(equalTo: bottomAnchor)
            ])

            // 添加place到视图
            addSubview(place)
            place.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                place.leadingAnchor.constraint(equalTo: textV.leadingAnchor, constant: 5),
                place.trailingAnchor.constraint(equalTo: textV.trailingAnchor),
                place.topAnchor.constraint(equalTo: textV.topAnchor, constant: 7)
            ])

            // 添加numberRemind到视图
            addSubview(numberRemind)
            numberRemind.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                numberRemind.trailingAnchor.constraint(equalTo: textV.trailingAnchor, constant: -5),
                numberRemind.bottomAnchor.constraint(equalTo: textV.bottomAnchor, constant: -7)
            ])

        textV.delegate = self
    }

    public func textViewDidChange(_ textView: UITextView) {
        updatePlaceVisibility()
        updateNumberRemind()
    }

    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        
        if maxNumber > 0 {
            // 限制输入字数
            let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
            return newText.count <= maxNumber
        }
        return true
    }
    
    
    
//    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
////            textField.insertText("\n")
//
//            return true
//        }

    private func updatePlaceVisibility() {
        place.isHidden = !textV.text.isEmpty
    }

    private func updateNumberRemind() {
        if maxNumber > 0 {
            let remainingCount = maxNumber - textV.text.count
            numberRemind.text = "\(remainingCount)/\(maxNumber)"
        } else {
            numberRemind.text = ""
        }
    }
}
